import * as XLSX from 'xlsx'; //直接引入就行


/**
 * 导出Excel
 * @param json   要导出的json数据
 * @param name   要导出的文件名
 * @param type   要导出的数据类型
 * @constructor
 */

export const ExportJosnToExcel = (json, name = 'data', type = 'application/octet-stream') => {
    let wb = { SheetNames: [], Sheets: {}, Props: { }};
    if (!Array.isArray (json)) {
        // eslint-disable-next-line no-param-reassign
        json = [json];
    }
    json.forEach (item => {
        wb.SheetNames.push (item.sheetName);
        wb.Sheets[item.sheetName] = XLSX.utils.json_to_sheet (item.sheetValues, item.sheetOptions);
    });
    const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };

    // eslint-disable-next-line no-use-before-define
    let blob = new Blob ([s2ab (XLSX.write (wb, wopts))], { type });

    let link = document.createElement ('a');

    document.body.appendChild (link);
    link.style.display = 'none';
    link.href = window.URL.createObjectURL (blob);
    link.download = `${name}.xlsx`;
    link.click ();
    // 释放资源
    setTimeout (() => {
        URL.revokeObjectURL (link.href);
    }, 100);

    function s2ab(s) {
        if (typeof ArrayBuffer !== 'undefined') {
            const buf = new ArrayBuffer (s.length);
            const view = new Uint8Array (buf);

            for (let i = 0; i !== s.length; ++i) {
                view[i] = s.charCodeAt (i) & 0xFF;
            }
            return buf;
        }
        const buf = new Array (s.length);

        for (let i = 0; i !== s.length; ++i) {
            buf[i] = s.charCodeAt (i) & 0xFF;
        }
        return buf;

    }
}

//   TS版本
// mixinExportJosnToExcel(json, name = 'data', type = 'application/octet-stream') {
//     let wb = { SheetNames: [], Sheets: {}, Props: { }};
//     if (!Array.isArray (json)) {
//         // eslint-disable-next-line no-param-reassign
//         json = [json];
//     }
//     json.forEach (item => {
//         wb.SheetNames.push (item.sheetName);
//         wb.Sheets[item.sheetName] = XLSX.utils.json_to_sheet (item.sheetValues, item.sheetOptions);
//     });
//     const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };
//
//     // eslint-disable-next-line no-use-before-define
//     let blob = new Blob ([s2ab (XLSX.write (wb, wopts))], { type });
//
//     let link = document.createElement ('a');
//
//     document.body.appendChild (link);
//     link.style.display = 'none';
//     link.href = window.URL.createObjectURL (blob);
//     link.download = `${name}.xlsx`;
//     link.click ();
//     // 释放资源
//     setTimeout (() => {
//         URL.revokeObjectURL (link.href);
//     }, 100);
//
//     function s2ab(s) {
//         if (typeof ArrayBuffer !== 'undefined') {
//             const buf = new ArrayBuffer (s.length);
//             const view = new Uint8Array (buf);
//
//             for (let i = 0; i !== s.length; ++i) {
//                 view[i] = s.charCodeAt (i) & 0xFF;
//             }
//             return buf;
//         }
//         const buf = new Array (s.length);
//
//         for (let i = 0; i !== s.length; ++i) {
//             buf[i] = s.charCodeAt (i) & 0xFF;
//         }
//         return buf;
//
//      }
// }
